package com.elvin.wx.controller;

import com.elvin.wx.beans.WirelessCommand;
import com.elvin.wx.service.WirelessCommandService;
import com.elvin.wx.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhenchl
 * @since 2022-02-17
 */
@Controller
@RestController
@RequestMapping("/wirelessCommand")
@Log4j2
@Api("无线缓存命令数据管理")
public class WirelessCommandController {
    @Resource
    private WirelessCommandService service;

    @ApiOperation(value = "获取所有缓存命令数据")
    @PostMapping("/queryAll")
    public Result<Object> queryAll() {
        Result<Object> result = new Result<>();
        try {
            List<WirelessCommand> selectList = service.selectList(null);
            result.success(selectList);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增缓存命令数据")
    @PostMapping("/insert")
    public Result<Object> insert(@RequestBody WirelessCommand entity) {
        Result<Object> result = new Result<>();
        try {
            service.insert(entity);
            result.success(entity, "新增成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "修改缓存命令数据")
    @PostMapping("/update")
    public Result<Object> update(@RequestBody WirelessCommand entity) {
        Result<Object> result = new Result<>();
        try {
            service.updateById(entity);
            result.success("修改成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除缓存命令数据 ")
    @PostMapping("/delete")
    public Result<Object> delete(@ApiParam(name="id",value="缓存命令数据id",required=true) @RequestParam("id")Long id) {
        Result<Object> result = new Result<>();
        try {
            service.deleteById(id);
            result.success("删除成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据ID获取缓存命令数据")
    @PostMapping("/getById")
    public Result<Object> getById(@ApiParam(name="id",value="缓存命令数据id",required=true) @RequestParam("id") Long id) {
        Result<Object> result = new Result<>();
        try {
            WirelessCommand entity = new WirelessCommand();
            entity.setId(id);
            entity = service.selectById(entity);
            result.success(entity);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }
}

